package com.weiling.config;

import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;


@Import(JdbcConfig.class)
@MapperScan("com.weiling.mapper")
@PropertySource("classpath:jdbc.properties")
public class MyBatisPlusConfig {
    /**
     * 配置SqlSessionFactoryBean
     *
     * @param dataSource
     * @return
     */
    @Bean
    public MybatisSqlSessionFactoryBean sqlSessionFactory(@Autowired DataSource dataSource) {
        MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);

        // 实体类类型别名
        factoryBean.setTypeAliasesPackage("com.weiling.entity");

        // 指定映射文件，如果映射文件和Mapper接口在同一个包下，可以省略该配置，使用mybatis注解也可以省略
//        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//        factoryBean.setMapperLocations(resolver.getResource("classpath:mapper/*Mapper.xml"));

        // mybatis设置
//        Configuration configuration = new Configuration();
//        configuration.setLazyLoadingEnabled(true);
//        configuration.setMapUnderscoreToCamelCase(true);
//        factoryBean.setConfiguration(configuration);

        // 引入mybatis主配置文件
//        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//        factoryBean.setConfigLocation(resolver.getResource("classpath:mybatis.xml"));

        return factoryBean;
    }

    /**
     * 配置Mapper接口扫描器
     *
     * @return
     */
//    @Bean
//    public MapperScannerConfigurer mapperScannerConfigurer() {
//        MapperScannerConfigurer msc = new MapperScannerConfigurer();
//        // 扫描的包
//        msc.setBasePackage("com.weiling.mapper");
//        return msc;
//    }
}
